/*
 * 文件名称: DefaultObjFactory.java
 * 版权信息: Copyright 2001-2012 ZheJiang Collaboration Data System Co., LTD. All right reserved.
 * ----------------------------------------------------------------------------------------------
 * 修改历史:
 * ----------------------------------------------------------------------------------------------
 * 修改原因: 新增
 * 修改人员: huangwb
 * 修改日期: 2012-3-5
 * 修改内容: 
 */
package com.huangwb.person.source;

import java.util.ArrayList;
import java.util.List;

import com.huangwb.person.bean.ArgInfo;
import com.huangwb.person.bean.Column;
import com.huangwb.person.bean.Obj;
import com.huangwb.person.bean.Prop;
import com.huangwb.person.util.CodeGenUtil;


/**
 * 
 * @author <a href="mailto:huangwb@zjcds.com">huangwb</a> created on 2012-3-5
 * @since DE6.0
 */
public class DefaultObjFactory {

    /**
     * @param argInfo
     * @param strTableName
     * @param columns
     * @return
     * @author huangwb created on 2012-3-5 
     * @since DE6.0
     */
    public Obj genObj(ArgInfo argInfo, List<Column> columns) {
        String name = "";
        String jdbcName = "";
        String jdbcType = "";
        String cName = argInfo.getClassName();
        String strTableName = argInfo.getTableName();
        String pck = argInfo.getPck();
        Obj obj = new Obj();
        obj.setClassName(cName);
        obj.setPak(pck);
        obj.setTableName(strTableName);
        List<Prop> props = new ArrayList<Prop>();
        for(Column column : columns) {
            jdbcName = column.getColumnName();
            name = CodeGenUtil.convertPropName(jdbcName);
            jdbcType = column.getJdbcType();
            jdbcType = convertJdbcType(jdbcType);
            props.add(
                    new Prop(
                            CodeGenUtil.convertJdbcToJava(jdbcType),//得到java类型
                            name,
                            jdbcType,//
                            jdbcName
                    )
           );
           if(column.getIsPrimary()) {
               obj.setPrimaryKey(name);
               obj.setJdbcPK(jdbcName);
           }
        }
        obj.setProps(props);
        return obj;
    }
    
    private String convertJdbcType(String jdbcType) {
        jdbcType = jdbcType.toUpperCase();
        jdbcType = jdbcType.replace("VARCHAR2", "VARCHAR").replace("NUMBER", "NUMERIC");//改为通用型
        jdbcType = jdbcType.replace("NUMBER", "NUMERIC");//.replace("INT", "INTEGER")
        return jdbcType;
    }
    
}
